package com.example.controller;

import com.example.common.Result;
import com.example.entity.Room;
import com.example.service.RoomService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/room")
public class RoomController {

    @Resource
    private RoomService roomService;

    @PostMapping("/add")
    public Result add(@RequestBody Room room) {
        // 添加默认图片路径
        if (room.getImagePath() == null || room.getImagePath().isEmpty()) {
            // 帅帅双床房特别处理
            if ("帅帅双床房".equals(room.getRoomType())) {
                room.setImagePath("ssscf.jpg");
            } else {
                // 根据房间类型设置默认图片
                String defaultImagePath = getDefaultRoomImagePath(room);
                room.setImagePath(defaultImagePath);
            }
        }
        
        roomService.add(room);
        return Result.success();
    }

    @PutMapping("/update")
    public Result update(@RequestBody Room room) {
        roomService.update(room);
        return Result.success();
    }

    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Long id) {
        roomService.deleteById(id);
        return Result.success();
    }

    @DeleteMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<Long> ids) {
        roomService.deleteBatch(ids);
        return Result.success();
    }

    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Long id) {
        Room room = roomService.selectById(id);
        return Result.success(room);
    }

    @GetMapping("/selectAll")
    public Result selectAll(Room room) {
        List<Room> list = roomService.selectAll(room);
        return Result.success(list);
    }

    @GetMapping("/selectPage")
    public Result selectPage(Room room,
                           @RequestParam(defaultValue = "1") Integer pageNum,
                           @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Room> pageInfo = roomService.selectPage(room, pageNum, pageSize);
        return Result.success(pageInfo);
    }

    @GetMapping("/selectByHotelId/{hotelId}")
    public Result selectByHotelId(@PathVariable Long hotelId) {
        List<Room> list = roomService.selectByHotelId(hotelId);
        return Result.success(list);
    }

    // 新增预订房间接口
    @PostMapping("/book/{roomId}")
    public Result bookRoom(@PathVariable Long roomId) {
        boolean result = roomService.bookRoom(roomId);
        if (result) {
            return Result.success("房间预订成功");
        } else {
            return Result.error("500","房间已被预订或不存在");
        }
    }

    /**
     * 根据房间类型设置默认图片
     */
    private String getDefaultRoomImagePath(Room room) {
        String roomType = room.getRoomType();
        Long hotelId = room.getHotelId();
        
        // 首先检查是否是长隆熊猫酒店的特殊房型
        if (hotelId != null && hotelId == 4) { // 假设4是长隆熊猫酒店的ID
            if (roomType.contains("酷酷双床")) {
                return "xmjd_kksf.jpg";
            } else if (roomType.contains("亲子")) {
                return "xmjd_qzkk.jpg";
            } else if (roomType.contains("家庭")) {
                return "xmjd_jtsf.jpg";
            }
        }
        
        // 通用房型
        if (roomType.contains("标准") || roomType.contains("双床")) {
            return "room_standard.jpg";
        } else if (roomType.contains("豪华") || roomType.contains("经典")) {
            return "room_deluxe.jpg";
        } else if (roomType.contains("套房")) {
            return "room_suite.jpg";
        }
        
        // 默认图片
        return "room-default.jpg";
    }
}